package org.pidan.basic.algorithm.maopao;

import java.util.Arrays;

/**
 * {@code @description}: 冒泡排序
 * 自己理解：先排出最大数往后放，从后往前排序
 * {@code @author}: huangyuxin
 * {@code @date}: 2023-11-20
 */
public class Client {

    public static void main(String[] args) {

//        Integer[] integers = new Integer[]{5, 10, 4, 11, 8, 3};
        Integer[] integers = new Integer[]{5, 10, 7, 11, 4, 8};
        Integer[] integers1 = sort(integers, 6);
//        Integer[] integers1 = sort2(integers, 6);
        String string = Arrays.toString(integers1);
        System.out.println(string);

    }

    /**
     * 普通版冒泡排序
     */
    public static Integer[] sort(Integer[] arr, int len) {
        int count = 0;
        if (len <= 1) {
            return arr;
        }

        for (int i = 0; i < len; i++) {
            for (int j = 0; j < len - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
                count++;
            }
        }

        System.out.println(count);
        return arr;
    }


    public static Integer[] sort2(Integer[] arr, int len) {
        int count = 0;
        if (len <= 1) {
            return arr;
        }

        boolean flag = false;
        for (int i = 0; i < len; i++) {
            flag = false;
            for (int j = 0; j < len - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                    flag = true;
                }
            }
            System.out.println((i + 1) + " 冒泡");
            if (!flag) {
                break;
            }
        }
        return arr;
    }

}
